package com.hanfengyi.web.controller;

import com.hanfengyi.hrm.AjaxResult;
import com.hanfengyi.util.RedisUtils;
import org.springframework.web.bind.annotation.*;

/**
 * User: Han
 * Date: 2020/2/18
 * Time: 15:11
 * Description:
 */
@RestController
@RequestMapping("/redis")
public class RedisController {
    @PostMapping("/set")
    public AjaxResult set(@RequestParam("key") String key, @RequestParam("value")String value){
        try {
            RedisUtils.INSTANCE.set(key, value);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！"+e.getMessage());
        }
    }

    /**
     * 可设置过期时间
     * @param key
     * @param seconds
     * @param value
     * @return
     */
    @PostMapping("/setex")
    public AjaxResult setex(@RequestParam("key") String key, @RequestParam("seconds") int seconds,@RequestParam("value")String value){
        try {
            RedisUtils.INSTANCE.setex(key,seconds,value);
            return AjaxResult.me().setSuccess(true).setMessage("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败！"+e.getMessage());
        }
    }

    @GetMapping("/get/{key}")
    public AjaxResult get(@PathVariable("key") String key){
        String result = RedisUtils.INSTANCE.get(key);
        return AjaxResult.me().setResultObj(result);
    }
}